package gmu.swe.project.registry;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import static java.util.concurrent.TimeUnit.*;
import gmu.swe.project.tracker.TrackerInfo;
import gmu.swe.project.utils.Helper;
import gmu.swe.project.io.*;

/**
 * To run this test, here are the arguments needed: 1) interval to send keep
 * alive messages to the registry Also, a -D java option of CONFIG_FILE_LOCATION
 * needs to be defined
 * 
 * The registry servers will be read from the config.xml file
 * 
 * @author Stacy Meyer
 * 
 */
public class RegistryTester3 {
	final static ScheduledExecutorService scheduler = Executors
			.newScheduledThreadPool(1);
	private static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
	static SimpleDateFormat dfm = new SimpleDateFormat(DATE_FORMAT_NOW);
	private static ArrayList<RegistryInfo> registryServers = null;
	static TrackerRegistration tr = null;
	public static void main(String[] args) {

		String intervalString = args[0];
		final int interval = Integer.parseInt(intervalString);
		readConfigFile();
		
		try {

			final Runnable keepAlive = new Runnable() {

				public void run() {
					Iterator<RegistryInfo> registryIter = registryServers.iterator();
					while (registryIter.hasNext()) {
						
						Date time = Helper.getCurrTime();
						System.out.println("RegistryTester3()::Current time: "
								+ dfm.format(time));
						System.out
								.println("RegistryTester3::Ping Registry (every "
										+ interval + " seconds)");
						try {
							RegistryInfo ri = (RegistryInfo) registryIter.next();
							String ip = ri.getIpAddress();
							Integer port = ri.getPort();
							
							Socket socketConnection = new Socket(ip, port);
							System.out.println("RegistryTester3::ip: " + ip);
							System.out.println("RegistryTester3::port: " + port);
							// output stream to write to the server
							ObjectOutputStream clientOutputStream = new ObjectOutputStream(
									socketConnection.getOutputStream());

							// incoming stream to receive from the server
							ObjectInputStream clientInputStream = new ObjectInputStream(
									socketConnection.getInputStream());

							TrackerInfo tiInput = new TrackerInfo();
							tiInput.setIpAddress("003");

							clientOutputStream.writeObject(tiInput);

							tr = (TrackerRegistration) clientInputStream
									.readObject();

							System.out
									.println("RegistryTester3()::trackerRegistration from server, size: "
											+ tr.getTrackerSet().size());

							// close the streams
							clientOutputStream.close();
							clientInputStream.close();
							socketConnection.close();

							System.out
									.println("RegistryTester3()::Client closed");
						} catch (Exception ex) {
							System.out
									.println("RegistryTester3::Exception thrown: "
											+ ex);
						}// catch
					}// while
					System.out.println("RegistryTester3::Done processing");
				}// run
			};
			scheduler.scheduleAtFixedRate(keepAlive, 1, interval, SECONDS);

		} catch (Exception ex) {
			System.out.println("RegistryTester3()::Error in RegistryTester3: "
					+ ex);
		}
	}// main

	private static void readConfigFile() {

		// read the config file
		ConfigFileParser parser = Helper.getParser();

		// get the IP addresses/ports of the Registry servers
		registryServers = parser.getRegistryServers();

	}// readConfigFile

}
